package cn.edu.scau.cmi.linjinyong.fxml.adapter;

import cn.edu.scau.cmi.linjinyong.adapter.StudentSortUtil02;
import cn.edu.scau.cmi.linjinyong.adapter.pojo.Student;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;

import java.util.ArrayList;
import java.util.List;

public class ObjectAdapterController {
    @FXML
    private TextField sid;
    @FXML
    private TextField sname;
    @FXML
    private TableColumn idList;
    @FXML
    private TableColumn nameList;
    @FXML
    private TableView<Student> studentList;

    private List<Student> students = new ArrayList<>();

    @FXML
    private void addStudent(){
        Student s = new Student(Integer.parseInt(sid.getText()), sname.getText());
        students.add(s);
        
        sid.setText("");
        sname.setText("");
        
        StudentSortUtil02 sortUtil = new StudentSortUtil02();
        students = sortUtil.sortStudent(students);
        idList.setCellValueFactory(new PropertyValueFactory<Student, Integer>("id"));
        nameList.setCellValueFactory(new PropertyValueFactory<Student, String>("name"));
        ObservableList datas = FXCollections.observableArrayList();
        datas.addAll(students);
        studentList.setItems(datas);
    }
}
